package com.donggua.aichebao.commonui.CustomTextView;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by Administrator on 2017-08-05.
 */
//com.donggua.aichebao.commonui.TipTextViewLayout
public class TipTextViewLayout extends LinearLayout implements View.OnClickListener {
    private static final int VERTICAL = 0;
    private static final int HORIZONTAL = 1;
    private static final int DEFAULT_SIZE = 15;
    private static final int DEFAULT_TEXT_COLOR = 0Xff000000;
    private boolean tpVIconLeft = false;
    private int margin = 2;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;

    private TextView tipText;
    private ImageView tipIcon;

    public TipTextViewLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        margin = (int) context.getResources().getDimension(R.dimen.margin);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TipTextView);
        tpVIconLeft = ta.getBoolean(R.styleable.TipTextView_tpVIconLeft, false);
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(Gravity.CENTER_VERTICAL);
        tipText = getTextView(context);
        String Text = ta.getString(R.styleable.TipTextView_android_text);
        int TextSize = (int) ta.getDimension(R.styleable.TipTextView_android_textSize, DEFAULT_SIZE);
        int Color = ta.getColor(R.styleable.TipTextView_android_textColor, DEFAULT_TEXT_COLOR);
        tipText.setText(Text);
        tipText.setTextSize(textSizeType, TextSize);
        tipText.setTextColor(Color);
        tipIcon = getIconView(context);

        int src = ta.getResourceId(R.styleable.TipTextView_android_src, R.mipmap.tpv_default_icon);
        if (src != -1) {
            tipIcon.setBackgroundResource(src);
        }

        if (tpVIconLeft == true) {
            LayoutParams firstLayoutParams = getMyLayoutParams(HORIZONTAL);
            tipText.setLayoutParams(firstLayoutParams);
            this.addView(tipIcon);
            this.addView(tipText);
        } else {
            LayoutParams secondLayoutParams = getMyLayoutParams(HORIZONTAL);
            tipIcon.setLayoutParams(secondLayoutParams);
            this.addView(tipText);
            this.addView(tipIcon);
        }

 	   ta.recycle();
       tipIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Test", "abcd");    }
        });
    }

    private TextView getTextView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(textSizeType, DEFAULT_SIZE);
        return textView;
    }

    private ImageView getIconView(Context context) {
        ImageView iconView = new ImageView(context);
        return iconView;
    }

    private LayoutParams getMyLayoutParams(int originStyle) {
        LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (originStyle == HORIZONTAL) {
            layoutParams.leftMargin = margin ;
        } else {
            layoutParams.topMargin = margin ;
        }
        return layoutParams;
    }

    public void setTipText(String text) {
        tipText.setText(text);
    }

    public void setImageResource(int resId) {
        tipIcon.setImageResource(resId);
    }

    public void setTipColor(int color) {
        tipText.setTextColor(color);
    }

    @Override
    public void onClick(View view) {
        Log.i("on", "clicked" + view.getId());
        if(view.getId() == tipIcon.getId())
        {
           Log.i("TEst", "clicked" + tipIcon.getId());
        }
    }
}
